package aaa.com.pp.http;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.ResponseBody;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by lisa on 2016/12/19.
 * Email: 457420045@qq.com
 */

public class HttpMethods_1 {
    public static final String BASE_URL = "http://gank.io/api/";
    private static final int DEFAULT_TIMEOUT = 5;

    private Retrofit retrofit;
    private RetrofitService retrofitService;

    private static class SingletonHolder {
        public static final HttpMethods_1 INSTANCE = new HttpMethods_1();
    }

    public static HttpMethods_1 getInstance() {
        return SingletonHolder.INSTANCE;
    }

    private HttpMethods_1() {
        //手动创建一个OkHttpClient并设置超时时间
        OkHttpClient.Builder okBulider = new OkHttpClient.Builder();
        okBulider.connectTimeout(DEFAULT_TIMEOUT, TimeUnit.MINUTES);

        retrofit = new Retrofit.Builder()
                .client(okBulider.build())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl(BASE_URL)
                .build();

        retrofitService = retrofit.create(RetrofitService.class);
    }

    public void getJsonData(Subscriber<ResponseBody> subscriber, String category, String pageSize, String pageNum) {
        retrofitService.getJsonData(category, pageSize, pageNum)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);

    }

}